include(${CMAKE_CURRENT_LIST_DIR}/check-data-dir.cmake)

if (NOT IS_DIRECTORY "${v1}/data/content")
  add_error("Custom content directory does not exist.")
endif()

file(GLOB content_files ${v1}/data/content/*)
list(LENGTH content_files num)
if (NOT ${num} EQUAL 2)
  add_error("Found ${num} custom content files, expected 2.")
endif()

macro(assert_key contents key expected)
  string(JSON value ERROR_VARIABLE errors GET "${contents}" ${key})
  if (errors)
    add_error("Did not find expected key \"${key}\" in custom content.")
  endif()
  if (NOT ${value} MATCHES ${expected})
    add_error("Unexpected data in custom content file:\nGot ${value}, Expected ${expected}.")
  endif()
endmacro()

# Check contents of configureContent files
set(firstFile "")
foreach(content_file IN LISTS content_files)
  read_json("${content_file}" contents)
  assert_key("${contents}" myString "string")
  assert_key("${contents}" myBool "OFF")
  assert_key("${contents}" myInt "1")
  assert_key("${contents}" myFloat "2.5")
  assert_key("${contents}" myTrue "ON")
  assert_key("${contents}" myList "[ \"a\", \"b\", \"c\" ]")
  assert_key("${contents}" myObject "{.*\"key\".*:.*\"value\".*}")
  if (NOT firstFile)
    set(firstFile "${content_file}")
  endif()
  if ("${content_file}" STREQUAL "${firstFile}")
    string(JSON firstN GET "${contents}" nConfigure)
  else()
    string(JSON secondN GET "${contents}" nConfigure)
  endif()
endforeach()

# Ensure provided -DN=* arguments result in differing JSON contents
math(EXPR expectedSecondN "3-${firstN}")
if (NOT ${secondN} EQUAL ${expectedSecondN})
  add_error("Configure content did not correspond to provided cache variables.\nGot: ${firstN} and ${secondN}")
endif()

# Ensure snippets reference valid files
foreach(snippet IN LISTS snippets)
  read_json("${snippet}" contents)
  string(JSON filename GET "${contents}" configureContent)
  if (NOT EXISTS "${v1}/data/${filename}")
    add_error("Reference to content file that does not exist.")
  endif()
endforeach()
